using Abp.AspNetCore;
using Batman.Cms.Core;
using Batman.Cms.Hosting;
using Batman.Cms.Loaders;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Batman.Cms
{
    public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment evn)
        {
            Configuration = configuration;

            _evn = evn;
        }

        private IHostingEnvironment _evn { get; }

        public IConfiguration Configuration { get; }

        public List<Type> TypeList = new List<Type>();

        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            var plugins = _evn.GetPlugins();

            var mvcBuilder = services.AddMvc().ConfigureApplicationPartManager(manager =>
            {
                var oldMetadataReferenceFeatureProvider = manager.FeatureProviders.First(f => f is MetadataReferenceFeatureProvider);
                manager.FeatureProviders.Remove(oldMetadataReferenceFeatureProvider);
                manager.FeatureProviders.Add(new ReferencesMetadataReferenceFeatureProvider());
            }).AddControllersAsServices()
              .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<RazorViewEngineOptions>, PluginRazorViewEngineOptionsSetup>());
            services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());

            plugins.ForEach(t => mvcBuilder.AddApplicationPart(t.Assembly));

            new MvcConfiguration().ConfigureMvc(mvcBuilder);

            services.ConfigureServices(_evn);

            return services.AddAbp<CmsModule>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseAbp();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMiddleware<PluginStaticFileMiddleware>();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseMvc(routes => routes.MapRoute(name: "Default", template: "{Controller}/{Action}", defaults: new { controller = "Test", action = "List" }));

            app.Configure(env);

        }
    }
}
